<template>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-form-item label="证书名称" prop="title">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="证书编号" prop="numbers">
            <el-input v-model="ruleForm.numbers" />
        </el-form-item>
        <el-form-item label="生效日期" prop="start_date">
            <el-date-picker v-model="ruleForm.start_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="失效日期" prop="end_date">
            <el-date-picker v-model="ruleForm.end_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="证书文件" prop="files">
            <el-button type="primary" size="small" @click="showUpload = true"> 选择文件 </el-button>
        </el-form-item>
        <el-form-item label=" " >
            <el-text>文件地址：{{ruleForm.files}}</el-text>
        </el-form-item>
    </el-form>
    <div class="mt20 text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
    </div>

    <el-dialog v-model="showUpload" title="文件">
        <component :is="uploadFile" :data="uploadData" @saveUpload="saveUpload" />
    </el-dialog>
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import http from '../../../../util/request';
import uploadFile from '@/components/uploadFile.vue'
const loading = ref<boolean>(false)
const showUpload = ref<boolean>(false)
let uploadData:any = ref({prefix:'cert/'})
interface RuleForm {
    id: number|string
    user_id:number
    title: string
    numbers: string
    type: number
    start_date: string
    end_date: string
    files: string
} 

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id: 0,
    user_id:0,
    title: '',
    numbers:'',
    files: '',
    type: 0,
    start_date: '',
    end_date: '',
})

const onGetData = () => {
    if(!ruleForm.value.id){
        return
    }
    http.get('/cert/'+ruleForm.value.id).then((response:any)=>{
        ruleForm.value = response.data.data
    })
}

const props = defineProps<{
    user_id:number
    id:number
}>()

watch(props,()=>{
    ruleForm.value.id = props.id
    ruleForm.value.user_id = props.user_id
    ruleForm.value.title = ''
    ruleForm.value.numbers = ''
    ruleForm.value.files = ''
    ruleForm.value.start_date = ''
    ruleForm.value.end_date = ''
    onGetData()
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
    title: [
        { required: true, message: '请输入名称', trigger: 'blur' },
    ],
    numbers: [
        { required: true, message: '请输入编号', trigger: 'blur' },
    ],
    start_date: [
        { required: true, message: '请输入生效日期', trigger: 'blur' },
    ],
    end_date: [
        { required: true, message: '请输入失效日期', trigger: 'blur' },
    ],
    files: [
        { required: true, message: '请选择证书', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            let request:any = null
            if(ruleForm.value.id){
                request = http.patch('/cert',ruleForm.value)
            }else{
                request = http.put('/cert',ruleForm.value)
            }
            request.then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);return
                }
                ElMessage.success(response.data.msg);
                onSave()  
            })
                      
        } else {
            console.log('error submit!', fields)
        }
    })
}

const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])

const saveUpload = (data:string) => {
    ruleForm.value.files = data
    showUpload.value = false
}
</script>
<style scoped>
</style>
